package com.rop.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rop.model.system.SysLoginLog;
import com.rop.model.vo.SysLoginLogQueryVo;
import com.rop.system.mapper.SysLoginLogMapper;
import com.rop.system.service.SysLoginLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Title: SysLoginLogServiceImpl
 * @Author Rop
 * @Date 2023/5/14 17:10
 * @description: 日志
 */

@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

    @Autowired
    private SysLoginLogMapper sysLoginLogMapper;

    /**
     * 记录登录日志
     * @param username 用户名
     * @param ipAddr ip地址
     * @param status 状态
     * @param message 信息
     */
    @Override
    public void recordLoginLog(String username, String ipAddr, Integer status, String message) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setUsername(username);
        sysLoginLog.setIpaddr(ipAddr);
        sysLoginLog.setStatus(status);
        sysLoginLog.setMsg(message);
        sysLoginLogMapper.insert(sysLoginLog);
    }

    /**
     * 分页列表查询
     *
     * @param sysLoginLogQueryVo vo
     * @return ""
     */
    @Override
    public IPage<SysLoginLog> selectPage(Long page, Long limit, SysLoginLogQueryVo sysLoginLogQueryVo) {

        Page<SysLoginLog> logPage = new Page<>(page,limit);

        QueryWrapper<SysLoginLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(sysLoginLogQueryVo.getUsername())) {
            queryWrapper.like("username", sysLoginLogQueryVo.getUsername());
        }
        if (StringUtils.isNotBlank(sysLoginLogQueryVo.getCreateTimeBegin())) {
            queryWrapper.ge("create_time", sysLoginLogQueryVo.getCreateTimeBegin());
        }
        if (StringUtils.isNotBlank(sysLoginLogQueryVo.getCreateTimeEnd())) {
            queryWrapper.le("create_time", sysLoginLogQueryVo.getCreateTimeEnd());
        }

        return baseMapper.selectPage(logPage, queryWrapper);
    }
}
